<?php

//add news entry

class Controller extends controllerBase {

	public function index()
	{
		$title = 'news';
		$content = 'this is the acp for the news module';
	}
	
	public function add()
	{
		global $headers;
		
		$title = 'news :: add';
		$errors = array();
	
		if(isset($_POST['submit']))
		{
			#process form
			
			$newnews = new news();
			
			#check title
			if($_POST['title'] == '')
			{
				$errors[] = 'no-title';
			}
			else
			{
				$newnews->title = $_POST['title'];
			}
			
			#check content
			if($_POST['content'] == '')
			{
				$errors[] = 'no-content';
			}
			else
			{
				$newnews->content = $_POST['content'];
			}
			
			if($_POST['url'] == '')
			{
				$errors[] = 'no-url';
			}
			else
			{
				$newnews->url = $_POST['url'];
			}
			
			
			$newnews->date = time();
			
			
			if(count($errors) <= 0)
			{
				$newnews->save();
				
				unset($_POST);
				$submitted = true;
			}
		}
		
		if($submitted)
		{
			#redirect
			$content = "news entry added";
		}
		else
		{
			#show form
			$content = form::getForm('news_add', array($errors));
			
		}
		
		include(ROOT . 'admin/skin/wrapper.php');
	}
	
	public function edit($id = '')
	{
		global $headers;
		
		$title = 'news :: edit';
		
		if($id == '')	//no page set, show all
		{
			$query = new Query("SELECT * FROM `news` ORDER BY `date` DESC");
			
			if($query->num_rows())
			{
				$news = news::RecordSet($query, 'news');
				
				for($i = 0; $i < count($news); $i++)
				{
					$content .= '<p>' . $news[$i]->title . ' : <a href="' . ROOT_URL .'admin/' . URL_BASE . 'news/edit/' . $news[$i]->id . '">Edit</a> - <a href="' . ROOT_URL .'admin/' . URL_BASE . 'news/delete/' . $news[$i]->id . '">Delete</a></p>';
				}
			}
			else
			{
				$content .= '<p>There are currently no news entries.</p>';
			}
		}
		else	//get that specific page for editing
		{
		
			$errors = array();
			
			$news = new news($id);
			
			$title .= ' :: ' . $news->title;
			
			if(isset($_POST['submit']))
			{
				#process form
				
				#check title
				if($_POST['title'] == '')
				{
					$errors[] = 'no-title';
				}
				else
				{
					$news->title = $_POST['title'];
				}
				
				#check content
				if($_POST['content'] == '')
				{
					$errors[] = 'no-content';
				}
				else
				{
					$news->content = $_POST['content'];
				}
				
				if($_POST['url'] == '')
				{
					$errors[] = 'no-url';
				}
				else
				{
					$news->url = $_POST['url'];
				}
				
				
				//$news->date = time();
				
				
				if(count($errors) <= 0)
				{
					$news->save();
										
					unset($_POST);
					$submitted = true;
				}
			}
			
			if($submitted)
			{
				#redirect
				$content .= "news entry updated";
			}
			else
			{
				#show form
				$content .= form::getForm('news_edit', array($errors, $news));
				
			}
		}
		
		include(ROOT . 'admin/skin/wrapper.php');
	}
	
	public function delete($id = '')
	{
		if($id != '')
		{
			$news = new news($id);
			$news->delete();
			$content = 'news deleted';
		}
		include(ROOT . 'admin/skin/wrapper.php');
	
	}
	
	
}
?>